package com.kefu.robot.service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.kefu.common.util.Assert;
import com.kefu.common.util.JacksonUtil;
import com.kefu.robot.mapper.RobotDocMapper;
import com.kefu.robot.model.RobotDoc;
import com.kefu.sys.model.SysOrgan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.robot.mapper.RobotDocTypeMapper;
import com.kefu.robot.model.RobotDocType;
import com.kefu.robot.vo.RobotDocTypeSearchVo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;

import javax.annotation.Resource;

@Service
@Transactional
public class RobotDocTypeService  extends AbstractKefuService<RobotDocType> {
    @Resource
    protected RobotDocTypeMapper robotDocTypeMapper;
    @Resource
    protected RobotDocMapper robotDocMapper;

    
    public List listTree(RobotDocTypeSearchVo search) {
        RobotDocType param = new RobotDocType();
        param.setTenantId(KefuContext.getKefuContext().getTenantId());
        param.setRobotNo(search.getRobotNo());
        param.setDocPattern(search.getDocPattern());
        List<RobotDocType> docTypeList = robotDocTypeMapper.select(param);
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        for (RobotDocType docType : docTypeList) {
            nodeList.add((new TreeNode<>(docType.getId(), docType.getParentId(), docType.getName(), docType.getWeight())));
        }
        return TreeUtil.build(nodeList, "0");
    }

    
    public int getMaxWeight(String tenantId, String parentId) {
        return robotDocTypeMapper.getMaxWeight(tenantId, parentId);
    }

    
    public Map<String, String> getDocTypeNameMap(String tenantId) {
        return list(Wrappers.lambdaQuery(RobotDocType.class).eq(RobotDocType::getTenantId, tenantId))
                .stream().collect(Collectors.toMap(RobotDocType::getId, RobotDocType::getName));

    }

    public Set<String> getAllNodes(String docTypeId) {
        Set<String> list = new HashSet<>();
        list.add(docTypeId);
        List<RobotDocType> docTypeList = this.list(Wrappers.lambdaQuery(RobotDocType.class)
                .eq(RobotDocType::getParentId, docTypeId)
                .eq(RobotDocType::getTenantId, KefuContext.getTid()));
        List<String> collect = docTypeList.stream().map(RobotDocType::getId).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            for (String id : collect) {
                Set<String> ids = getAllNodes(id);
                list.addAll(ids);
            }
        }
        return list;
    }

    public void getParent(RobotDocType robotDocType, List<RobotDocType> list) {
        if (Objects.isNull(robotDocType)) {
            return;
        }
        list.add(robotDocType);
        if (!"0".equals(robotDocType.getParentId())) {
            String parentId = robotDocType.getParentId();
            RobotDocType parentType = robotDocTypeMapper.selectByPrimaryKey(parentId);
            getParent(parentType, list);
        }
    }


    @Override
    public void deleteById(String id) {
        RobotDocType robotDocType = robotDocTypeMapper.selectByPrimaryKey(id);
        Assert.isNull(robotDocType, "分类已被删除！");
        Long parentCount = robotDocTypeMapper.selectCount(Wrappers.lambdaQuery(RobotDocType.class)
                .eq(RobotDocType::getParentId, id));
        Assert.isTrue(parentCount > 0, "删除失败，存在下级分类！");
        Long robotDocCount = robotDocMapper.selectCount(Wrappers.lambdaQuery(RobotDoc.class)
                .eq(RobotDoc::getDocTypeId, id));
        Assert.isTrue(robotDocCount > 0, "删除失败！");
        deleteById(id);
        logger.info("删除知识库分类:{}, {}", id, JacksonUtil.toString(robotDocType));
    }
}
